package servidor;

import common.RemoteInterface;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import javax.swing.JOptionPane;
import servidor.impl.RemoteImpl;

/**
 *
 * @author 
 */
public class servidor extends javax.swing.JFrame {

    /**
     * Creates new form servidor
     */
    public servidor() {
        initComponents();
        setLocation(350, 250);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jOptionPane1 = new javax.swing.JOptionPane();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("servidor/i18n/messages"); // NOI18N
        setTitle(bundle.getString("servidor.title")); // NOI18N

        //java.util.ResourceBundle bundle1 = java.util.ResourceBundle.getBundle("servidor/i18n/messages"); // NOI18N
        //jLabel1.setText(bundle1.getString("servidor.antesDeIniciar.text")); // NOI18N
        jLabel1.setText(bundle.getString("servidor.antesDeIniciar.text")); // NOI18N

        //jButton1.setText(bundle1.getString("servidor.botonIniciar.text")); // NOI18N
	jButton1.setText(bundle.getString("servidor.botonIniciar.text")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        //jButton2.setText(bundle1.getString("servidor.botondetener.text")); // NOI18N
        jButton2.setText(bundle.getString("servidor.botondetener.text")); // NOI18N
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("servidor/i18n/messages"); // NOI18N
         	      
        try {
            System.out.println("Iniciant servidor RMI...");
            RemoteInterface objetoRemoto = new RemoteImpl();
            Naming.rebind("//localhost/RemoteInterface", objetoRemoto );
            System.out.println("¡Servidor iniciat!");
            
            jButton1.setEnabled(false);
            jButton2.setEnabled(true);
            jLabel1.setText(bundle.getString("servidor.despuesDeiniciar.text"));
        } catch (RemoteException e) {		
            //No se ha podido conectar con RMI
            //e.printStackTrace();
            JOptionPane.showMessageDialog(rootPane, bundle.getString("error.rmiregistryNoEncontrado"),
                        bundle.getString("error.titleConexionRMI"), JOptionPane.WARNING_MESSAGE);
            System.exit(0);
        } catch (MalformedURLException e) {	
            //La URL es incorrecta
            //e.printStackTrace();
            JOptionPane.showMessageDialog(rootPane, bundle.getString("error.URLincorrecta"),
                        bundle.getString("error.titleConexionRMI"), JOptionPane.WARNING_MESSAGE);
            System.exit(0);
        } catch (Exception e){		
            //Error inesperado
            e.printStackTrace();
            System.exit(0);
        }                
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        System.exit(0);                             
    }//GEN-LAST:event_jButton2ActionPerformed
        
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        new servidor().setVisible(true);

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JOptionPane jOptionPane1;
    // End of variables declaration//GEN-END:variables
}
